package edu.olin.scheduler.gui.schedule;

import edu.olin.scheduler.common.*;
import edu.olin.scheduler.gui.GUIPrefs;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;

public class MeetingDisplay extends JTextArea implements Comparable, GUIPrefs.Listener {
    
    private Meeting meeting;
    private boolean extendsBack = true; //do we display an arrow indicating that this MeetingDisplay continues above the screen?
    private boolean extendsForwards = true; //do we display an arrow indicating that this MeetingDisplay continues below the screen?
    
    private GUIArrow arrowBack=null, arrowForwards=null; //GUI extension arrows
    private static int arrowSpacing = 5; //arrow spacing in px
    private static int arrowSize = 14; //arrow long dimension in px
    
    public boolean extendsBack(){ return extendsBack; }
    public boolean extendsForwards(){ return extendsForwards; }
    public void setExtendsBack(boolean extendsBack){ this.extendsBack = extendsBack; repaint(); }
    public void setExtendsForwards(boolean extendsForwards){ this.extendsForwards = extendsForwards; repaint(); }
    
    public MeetingDisplay(Meeting meeting){
        if(meeting==null){ throw new IllegalArgumentException("Cannot display a null meeting"); }
        this.meeting = meeting;
        arrowBack = new GUIArrow(arrowSize,-arrowSize/2); //this one points up
        arrowForwards = new GUIArrow(arrowSize,arrowSize/2); //this one points down
        setEditable(false); //not user-editable
        setSelectedTextColor(getForeground()); //hide selections
        setSelectionColor(getBackground()); //hide selections
        setLineWrap(true); setWrapStyleWord(true); //wrap by word, not by character
        setBorder(new LineBorder(Color.BLACK,1)); //1px black border
        GUIPrefs.addListener(this);
        guiPrefsChanged();
    }
    
    private void showMeetingText(){
        String text = "";
        if(GUIPrefs.getShowMeetingTimes()){
            text = meeting.start().toString(GUIPrefs.is24hour())+"-"+meeting.end().toString(GUIPrefs.is24hour())+"\n";
            }
        text += meeting.owner().getName();
        if(meeting.owner().getCategory().isCourse){ //show the section too
            text += "\nSection "+meeting.owner().getSection();
            }
        super.setText(text);
    }
    
    public void layoutArrows(){
        arrowBack.setX(getWidth()-arrowBack.getWidth()-arrowSpacing);
        arrowBack.setY(arrowForwards.getHeight()+arrowSpacing);
        arrowForwards.setX(getWidth()-arrowForwards.getWidth()-arrowSpacing);
        arrowForwards.setY(getHeight()-arrowForwards.getHeight()-arrowSpacing);
    }
    
    public void paint(Graphics g){
        super.paint(g);
        layoutArrows();
        if(extendsBack){ arrowBack.paint(g); }
        if(extendsForwards){ arrowForwards.paint(g); }
    }
    
    public int compareTo(Object other){
        MeetingDisplay md = (MeetingDisplay) other;
        return getMeeting().start().compareTo(md.getMeeting().start());
        }
    
    public Meeting getMeeting(){ return meeting; }
    
    public void setText(String text){ return; } //text cannot be changed
    public void append(String str){ return; } //text cannot be changed

    public void guiPrefsChanged(){
        showMeetingText(); //display the meeting text
        layoutArrows(); //setup our arrows
        setFont(GUIPrefs.getMeetingFont()); //pick a nice sans-serif font
        }
    
}